package cn.stylefeng.guns.modular.business.frequency.orderstatuschange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.custom.enums.OrderStatusEnum;
import cn.stylefeng.guns.modular.business.frequency.orderstatuschange.entity.OrderStatusChange;
import cn.stylefeng.guns.modular.business.frequency.orderstatuschange.mapper.OrderStatusChangeMapper;
import cn.stylefeng.guns.modular.business.frequency.orderstatuschange.model.param.OrderStatusChangeParam;
import cn.stylefeng.guns.modular.business.frequency.orderstatuschange.service.OrderStatusChangeService;
import cn.stylefeng.guns.modular.business.frequency.orderstatuschange.enums.OrderStatusChangeExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * tt_order_status_change 服务实现类
 *
 * @author yujie
 * @date 2023/07/11 15:44
 */
@Service
public class OrderStatusChangeServiceImpl extends ServiceImpl<OrderStatusChangeMapper, OrderStatusChange> implements OrderStatusChangeService {

    @Override
    public PageResult<OrderStatusChange> page(OrderStatusChangeParam orderStatusChangeParam) {

        // 构造条件
        LambdaQueryWrapper<OrderStatusChange> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public void saveOrderStatusChange(Integer beforeStatus, Integer afterStatus, Long orderId, String remark) {
        OrderStatusChange orderStatusChange = new OrderStatusChange();
        orderStatusChange.setBeforeChangeStatus(beforeStatus);
        orderStatusChange.setAfterChangeStatus(afterStatus);
        orderStatusChange.setOrderId(orderId);
        orderStatusChange.setRemark(remark);
        this.save(orderStatusChange);

    }

    @Override
    public List<OrderStatusChange> list(OrderStatusChangeParam orderStatusChangeParam) {

        // 构造条件
        LambdaQueryWrapper<OrderStatusChange> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderStatusChangeParam orderStatusChangeParam) {

        // 将dto转为实体
        OrderStatusChange orderStatusChange = new OrderStatusChange();
        BeanUtil.copyProperties(orderStatusChangeParam, orderStatusChange);

        this.save(orderStatusChange);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderStatusChangeParam orderStatusChangeParam) {
        this.removeById(orderStatusChangeParam.getId());
    }

    @Override
    public void edit(OrderStatusChangeParam orderStatusChangeParam) {

        // 根据id查询实体
        OrderStatusChange orderStatusChange = this.queryOrderStatusChange(orderStatusChangeParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderStatusChangeParam, orderStatusChange);

        this.updateById(orderStatusChange);
    }

    @Override
    public OrderStatusChange detail(OrderStatusChangeParam orderStatusChangeParam) {
        return this.queryOrderStatusChange(orderStatusChangeParam);
    }



    /**
     * 获取tt_order_status_change
     *
     * @author yujie
     * @since 2023/07/11 15:44
     */
    private OrderStatusChange queryOrderStatusChange(OrderStatusChangeParam orderStatusChangeParam) {
        OrderStatusChange orderStatusChange = this.getById(orderStatusChangeParam.getId());
        if (ObjectUtil.isEmpty(orderStatusChange)) {
            throw new ServiceException(OrderStatusChangeExceptionEnum.NOT_EXISTED);
        }
        return orderStatusChange;
    }
}